// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: daniel

#pragma once

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

namespace coin2::exchange::bithumb::api_util {

bool RateLimitPreventMode();

struct OrderCache {
  std::string order_id;
  std::string currency;
  std::string payment_currency;

  OrderCache() = default;
  OrderCache(
      const std::string& order_id_,
      const std::string& currency_,
      const std::string& payment_currency_)
      : order_id(order_id_), currency(currency_), payment_currency(payment_currency_) {}
  OrderCache(const OrderCache& order_cache) = default;
  OrderCache& operator=(const OrderCache& order_cache) = default;

  bool IsEmpty() const { return order_id.empty() || currency.empty() || payment_currency.empty(); }
};

class OrderManager {
 public:
  using OrderCachePtr = std::shared_ptr<OrderCache>;
  using OrderMap = std::unordered_map<std::string, OrderCachePtr>;

 public:
  OrderManager() = default;
  OrderManager(const OrderManager&) = delete;
  OrderManager& operator=(const OrderManager&) = delete;

  OrderMap GetOpenOrdersCopy() const;
  void ProcessOpenOrder(const std::string& order_id, const std::string& currency);
  void ProcessClosedOrder(const std::string& order_id);
  bool Find(const std::string& order_id) const;

 private:
  void AddOpenOrder(const OrderCachePtr& order);
  void DelOpenOrder(const std::string& order_id);

 private:
  OrderMap open_orders_map_;
};

}  // namespace coin2::exchange::bithumb::api_util
